package com.erp.common.web;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.framework.controller.form.SimpleUserStyleForm;
import com.erp.framework.util.CommonUtil;

public class ChangePasswordForm extends SimpleUserStyleForm {
    String companyCode;
    String userCode;
    String oldPasswd;
    String newPasswd;
    String confirmNewPasswd;

    public void setCompanyCode(String companyCode) {
        this.companyCode = companyCode;
    }

    public void setConfirmNewPasswd(String confirmNewPasswd) {
        this.confirmNewPasswd = confirmNewPasswd;
    }

    public void setNewPasswd(String newPasswd) {
        this.newPasswd = newPasswd;
    }

    public void setOldPasswd(String oldPasswd) {
        this.oldPasswd = oldPasswd;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getCompanyCode() {
        return companyCode;
    }

    public String getConfirmNewPasswd() {
        return confirmNewPasswd;
    }

    public String getNewPasswd() {
        return newPasswd;
    }

    public String getOldPasswd() {
        return oldPasswd;
    }

    public String getUserCode() {
        return userCode;
    }

    public final static String[] reservedKeyWords = { ProjectConstants.ACCESS_ALL, ProjectConstants.ACCESS_OWNER };

    public void otherValidate(ActionErrors errors) {   
    	
    	

        if (CommonUtil.isEmpty(oldPasswd)) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.field.blank", "Old Password"));
        } else {
            if (!oldPasswd.equals(oldPasswd.trim())) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.password.whitespace"));
            } else {
            	oldPasswd = oldPasswd.trim().toUpperCase();
            	
            	SelectDelegate sdg = new SelectDelegate();
            	String query = "select upper(password) from t_user where user_code = '"+userCode+"' and company_code = '"+companyCode+"'";
            	String dbPassword = CommonUtil.nullToString(String.valueOf(sdg.selectOneValue(query)));
            	// wrong old password
            	if (!dbPassword.equals(oldPasswd)) {
            		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.password.oldPassword"));
            	}
            }
        }

        if (CommonUtil.isEmpty(newPasswd)) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.field.blank", "New Password"));
        } else {
            if (!newPasswd.equals(newPasswd.trim())) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.password.whitespace"));
            }
        }

        if (CommonUtil.isEmpty(confirmNewPasswd)) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.field.blank", "Confirm New Password"));
        } else {
            if (!confirmNewPasswd.equals(confirmNewPasswd.trim())) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.password.whitespace"));
            }
        }

        if (!CommonUtil.isEmpty(newPasswd) && !CommonUtil.isEmpty(confirmNewPasswd)) {
            if (!newPasswd.toUpperCase().equals(confirmNewPasswd.toUpperCase())) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.login.password.not_matched"));
            }
        }
    }

}
